﻿@model BestCustomersReportModel
@inject AdminAreaSettings adminAreaSettings

<vc:admin-widget widget-zone="customer_details_report_by_number_of_orders_top" additional-data="Model" />
<div class="form-horizontal">
    <div class="form-body">
        <div class="form-group">
            <admin-label asp-for="StartDate" />
            <div class="col-md-9 col-sm-9">
                <admin-input asp-for="StartDate" />
            </div>
        </div>
        <div class="form-group">
            <admin-label asp-for="EndDate" />
            <div class="col-md-9 col-sm-9">
                <admin-input asp-for="EndDate" />
            </div>
        </div>
        <div class="form-group">
            <admin-label asp-for="OrderStatusId" />
            <div class="col-md-9 col-sm-9">
                <admin-select asp-for="OrderStatusId" asp-items="Model.AvailableOrderStatuses" />
            </div>
        </div>
        <div class="form-group">
            <admin-label asp-for="PaymentStatusId" />
            <div class="col-md-9 col-sm-9">
                <admin-select asp-for="PaymentStatusId" asp-items="Model.AvailablePaymentStatuses" />
            </div>
        </div>
        <div class="form-group">
            <admin-label asp-for="ShippingStatusId" />
            <div class="col-md-9 col-sm-9">
                <admin-select asp-for="ShippingStatusId" asp-items="Model.AvailableShippingStatuses" />
            </div>
        </div>
    </div>
    <div class="form-actions">
        <div class="col-md-offset-3 col-sm-offset-3 col-md-9 col-sm-9">
            <button class="btn btn-success filter-submit" id="search-best-customers-numberoforders-grid">
                <i class="fa fa-search"></i> @T("Admin.Reports.Customers.RunReport")
            </button>
        </div>
    </div>

</div>
<div id="best-customers-numberoforders-grid"></div>
<vc:admin-widget widget-zone="customer_details_report_by_number_of_orders_bottom" additional-data="Model" />
<script>
$(document).ready(function () {
    $("#best-customers-numberoforders-grid").kendoGrid({
        dataSource: {
            transport: {
                read: {
                    url: "@Html.Raw(Url.Action("ReportBestCustomersByNumberOfOrdersList", "Reports"))",
                    type: "POST",
                    dataType: "json",
                    data: bestCustomersNumberOfOrdersAdditionalData
                }
            },
            schema: {
                data: "Data",
                total: "Total",
                errors: "Errors"
            },
            error: function (e) {
                display_kendoui_grid_error(e);
                // Cancel the changes
                this.cancelChanges();
            },
            pageSize: @(adminAreaSettings.DefaultGridPageSize),
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true
        },
        pageable: {
            refresh: true,
            pageSizes: [@(adminAreaSettings.GridPageSizes)]
        },
        editable: {
            confirmation: false,
            mode: "inline"
        },
        scrollable: false,
        columns: [{
            field: "CustomerName",
            title: "@T("Admin.Reports.Customers.BestBy.Fields.Customer")",
            template: '<a class="k-link" href="@Url.Content("~/Admin/Customer/Edit/")#=CustomerId#">#:CustomerName#</a>',
        }, {
            field: "OrderTotal",
            title: "@T("Admin.Reports.Customers.BestBy.Fields.OrderTotal")"
        }, {
            field: "OrderCount",
            title: "@T("Admin.Reports.Customers.BestBy.Fields.OrderCount")"
        }]
    });
});
</script>
<script type="text/javascript">
$(document).ready(function () {

    $('#search-best-customers-numberoforders-grid').click(function () {
        var grid = $('#best-customers-numberoforders-grid').data('kendoGrid');
        grid.dataSource.page(1); //new search. Set page size to 1
        //grid.dataSource.read(); we already loaded the grid above using "page" function
        return false;
    });

});
function bestCustomersNumberOfOrdersAdditionalData(e) {
    var data = {
        StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
        EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
        OrderStatusId: $('#@Html.FieldIdFor(model => model.OrderStatusId)').val(),
        PaymentStatusId: $('#@Html.FieldIdFor(model => model.PaymentStatusId)').val(),
        ShippingStatusId: $('#@Html.FieldIdFor(model => model.ShippingStatusId)').val()
    };
    addAntiForgeryToken(data);
    return data;
}
</script>

